'''
Created on 21.01.2013

@author: aska
'''
import sys
import Door_control_dll_interface
import ctypes

from twisted.internet import reactor
from twisted.python import log

from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS
 
global Door_control_dll_interface_inst
Door_control_dll_interface_inst = Door_control_dll_interface.Door_control_dll_interface()
 
class EchoServerProtocol(WebSocketServerProtocol):
    #Door_control_dll_interface_inst = 0
    def onMessage(self, msg, binary):
        ret_message = msg
        print "sending echo:", msg
        if msg == 'command_1':
            print '* command 1'
        if msg[0:14] == 'command_test_0' :
            ans = '-'
            if len(msg) >= 17:
                arg_1 = int(msg[15])
                arg_2 = int(msg[17])
                ans = arg_1 + arg_2
                #var_res = 0
                #Door_control_dll_interface_inst.func_door_control_get_sum(arg_1, arg_2, ans)
                ret_message += ' ' + str(ans)
            else:
                ret_message += ' incorrect string'
        if msg[0:16] == 'command_dll_init' :
            ser_name = '//./COM50'
            ret_val = 0
            ret_val = Door_control_dll_interface_inst.func_door_control_init()
            print 'init ', ret_val
            ret_val = Door_control_dll_interface_inst.func_door_control_ser_set_name(ser_name)
            print 'set_name ', ret_val
            ret_val = Door_control_dll_interface_inst.func_door_control_dev_open()
            ret_message += ' result :' + str(ret_val)
            print 'dev_open ', ret_val
        if msg[0:17] == 'command_lock_open' :
            ret_val = 0
            ret_val = Door_control_dll_interface_inst.func_door_control_lock_set(1)
            print 'lock_set(1) ', ret_val
            ret_message += ' result :' + str(ret_val)
        if msg[0:18] == 'command_lock_close' :
            ret_val = 0
            ret_val = Door_control_dll_interface_inst.func_door_control_lock_set(0)
            print 'lock_set(0) ', ret_val
            ret_message += ' result :' + str(ret_val)
        if msg == 'command_inps_r':
            ans = '00'
            ans_int = Door_control_dll_interface_inst.func_door_control_inps_get()
            print 'inps_r, ans ', hex(ans_int)
            ret_message += ' ' + hex(ans_int)
        if msg[0:17] == 'command_modem_res' :
            ret_val = 0
            ret_val = Door_control_dll_interface_inst.func_door_control_modem_reset()
            print 'modem_reset() ', ret_val
            ret_message += ' result :' + str(ret_val)
        if msg[0:17] == 'command_dll_close' :
            ret_val = 0
            ret_val = Door_control_dll_interface_inst.func_door_control_finish()
            ret_message += ' result :' + str(ret_val)

        self.sendMessage(ret_message, binary)
             
 
 
if __name__ == '__main__':
    #log.startLogging(sys.stdout)
    #print ctypes.windll.kernel32
    #door_control_dll_inst = ctypes.cdll("../Door_control_dll/Release/Door_control_dll.dll")
    #Door_control_dll_interface_inst = Door_control_dll_interface.Door_control_dll_interface()
    #ret_val = Door_control_dll_interface_inst.func_door_control_init()
    #print 'init ', ret_val
    #ret_val = Door_control_dll_interface_inst.func_door_control_ser_set_name('//./COM50')

    factory = WebSocketServerFactory("ws://localhost:9000", debug = True)
    factory.protocol = EchoServerProtocol
    listenWS(factory)
    reactor.run()
